package com.group.library.order.controller.user;

import com.group.library.order.dto.MessageBean;
import com.group.library.order.entity.User;
import com.group.library.order.entity.UserLogin;
import com.group.library.order.service.UserBaseService;
import com.group.library.order.util.MessageUtil;
import com.group.library.order.util.RedisTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user/base")
public class UserBaseController {

    @Autowired
    private UserBaseService userBaseService;

    @Autowired
    private HttpServletRequest request;

    // 获取用户基本信息
    @GetMapping("/getUserInfo.action")
    public MessageBean getUserInfo(String name,String phone,String number){
        System.out.println("进来了\nname:"+name+" phone:"+phone+" number:"+number);
        return MessageUtil.success("获取成功",userBaseService.getUserInfo());
    }

    //验证用户登录
    @GetMapping("/login")
    public MessageBean userLogin(String number, String password){

        HttpSession session = request.getSession();

        final UserLogin userLogin = userBaseService.ExitUser(number, password);

        if(session.getAttribute("userNum") == null) {
            if (userLogin!=null){

                session.setAttribute("userId", userLogin.getUserId());
                //设置会话过期时间为30天
                session.setMaxInactiveInterval(30 * 24 * 60 * 60);

                return MessageUtil.success("登录成功！！！");
            }
        }else {
            return MessageUtil.success("登录成功！！！");
        }
        return MessageUtil.failure("用户不存在！！！");
    }

    //显示用户详细
    @GetMapping("/showUserInfo")
    public MessageBean showUserInfo(){
        final HttpSession session = request.getSession();
        if(session.getAttribute("userId")!=null)
        return MessageUtil.success(userBaseService.showInfo());
        return MessageUtil.failure("未登录！！！");
    }

    //修改基本信息
    @GetMapping("/modifyUser")
    public MessageBean modifyUser(String name,String number,String faculty,String grade,String phone){
//        final HttpSession session = request.getSession();
//        final String userId = (String)session.getAttribute("userId");
        String userId = "0h0jjisadjias00";
        if(userId!=null){
            final User user = userBaseService.modifyUser(userId, name, number, faculty, grade, phone);
            if(user!=null){
                return MessageUtil.success("修改成功！！！",user);
            }
        }
        return MessageUtil.failure("修改失败！！！");
    }

    //获取座位详细信息
    @GetMapping("/getSeatInfo")
    public MessageBean getSeatInfo(boolean flag){
        if(flag==true){
            return MessageUtil.success(userBaseService.getSeatInfo());
        }else{
            return MessageUtil.failure("获取失败！！！");
        }
    }


    //修改密码
//    @GetMapping("/modifyPwd")
//    public MessageBean userModify(String newPwd){
//
//        HttpSession session = request.getSession();
//        String userNum = (String)session.getAttribute("userNum");
//        String userNum="1914080902511";
//        if(userNum!=null) {
//            if (userBaseService.modifyResult(newPwd,userNum)) {
//                return MessageUtil.success("修改成功！！！");
//            }
//        }
//        return MessageUtil.failure("修改失败！！！");
//    }

    //获取公告信息
    @GetMapping("/getNoticeInfo")
    public MessageBean getNoticeInfo(){
        final HttpSession session = request.getSession();
        if(session.getAttribute("userId")!=null&&userBaseService.getNoticeInfo().getStatus()==0){
            return MessageUtil.success(userBaseService.getNoticeInfo());
        }
        return MessageUtil.failure("用户未登录！！");
    }
}
